package net.sk.china.city.talk.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sk.china.city.talk.model.system.SystemCity;
import net.sk.china.city.talk.request.system.SystemCitySearchRequest;
import net.sk.china.city.talk.response.system.SystemCityTreeResponse;
import net.sk.china.city.talk.service.system.SystemCityService;
import net.sk.china.common.response.CommonResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/3 16:24
 * // +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("system/city/")
@Api(tags = "城市管理")
public class SystemCityController {

    private final SystemCityService systemCityService;

    public SystemCityController(SystemCityService systemCityService) {
        this.systemCityService = systemCityService;
    }


    @PreAuthorize("hasAuthority('admin:system:city:list')")
    @ApiOperation(value = "列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult<Object> getList(@Validated SystemCitySearchRequest request) {
        return CommonResult.success(systemCityService.getList(request));
    }


    @PreAuthorize("hasAuthority('admin:system:city:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestParam Integer id, @Validated SystemCitySearchRequest request) {
        if (systemCityService.update(id, request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @PreAuthorize("hasAuthority('admin:system:city:update:status')")
    @ApiOperation(value = "修改状态")
    @RequestMapping(value = "update/status", method = RequestMethod.POST)
    public CommonResult<String> updateStatus(@RequestParam Integer id, @RequestParam Boolean status) {
        if (systemCityService.updateStatus(id, status)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @PreAuthorize("hasAuthority('admin:system:city:info')")
    @ApiOperation(value = "详情")
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public CommonResult<SystemCity> info(@RequestParam(value = "id") Integer id) {
        return CommonResult.success(systemCityService.getById(id));
    }


    @PreAuthorize("hasAuthority('admin:system:city:list:tree')")
    @ApiOperation(value = "获取tree结构的列表")
    @RequestMapping(value = "list/tree", method = RequestMethod.GET)
    public CommonResult<List<SystemCityTreeResponse>> getListTree() {
        return CommonResult.success(systemCityService.getListTree());
    }
}
